package cn.duhengkui.national.poi.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import cn.duhengkui.national.valuelist.core.entity.BaseEntity;
import cn.duhengkui.national.valuelist.core.entity.BaseObject;

/**   
 * <table border="1">
 *	<tr><td>包名称：</td><td>com.zhibo.tj.tt.dwkh.utils</td></tr>
 *	<tr><td>类名称：</td><td>ChangeBean</td></tr>
 *	<tr><td>类描述：</td><td></td></tr>
 *	<tr><td>创建人：</td><td>杜恒奎</td></tr>
 *  <tr><td>创建时间：</td><td>2016年11月11日/上午11:38:41</td></tr> 
 *  <tr><td>版本：</td><td>1.0.0</td></tr>
 * </table>   
 *     
 */
public class ChangeBean {
	
	public static Object change(BaseEntity fromObject,Class<? extends BaseObject> fromClass,Class<? extends Object> toClass) throws InstantiationException, IllegalAccessException{
		Field[] fieldsFrom = fromClass.getDeclaredFields();
		Field[] fieldsTo = toClass.getDeclaredFields();
		Object toObject = toClass.newInstance();
		for (int j = 0; j < fieldsTo.length; j++) {
			boolean match = false;
			try {
				for (int k = 0; k < fieldsFrom.length; k++) {
					if (fieldsTo[j].getName().equals(fieldsFrom[k].getName())) {

						Method setter = toClass.getDeclaredMethod(
								"set" + captureName(fieldsTo[j].getName()), fieldsTo[j].getType());
						Method getter = fromClass
								.getDeclaredMethod("get" + captureName(fieldsFrom[k].getName()));
						Object value = getter.invoke(fromObject);
						if(value != null){
							setter.invoke(toObject, getter.invoke(fromObject));
						}else{
							String colName = generName(fieldsTo[j].getName());
							setter.invoke(toObject, fromObject.getExtendProperties().get(colName));
						}
						match = true;
						break;
					}
				}
				if (!match) {
					String colName = generName(fieldsTo[j].getName());
					Method setter = toClass.getDeclaredMethod(
							"set" + captureName(fieldsTo[j].getName()), fieldsTo[j].getType());
					Object object = fromObject.getExtendProperties().get(colName);
					if(object != null){
						setter.invoke(toObject, fromObject.getExtendProperties().get(colName));
					}
					
				}
			} catch (Exception e) {}
		}
		return toObject;
	}
	
	public static List<? extends Object> changeList(List<? extends BaseEntity> list,Class<? extends BaseEntity> fromClass,Class<? extends Object> toClass) throws InstantiationException, IllegalAccessException {
		List<Object> resultList = new ArrayList<Object>();
		Field[] fieldsFrom = fromClass.getDeclaredFields();
		Field[] fieldsTo = toClass.getDeclaredFields();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				BaseEntity fromObject = list.get(i);
				Object toObject = toClass.newInstance();
				for (int j = 0; j < fieldsTo.length; j++) {
					boolean match = false;
					try {
						for (int k = 0; k < fieldsFrom.length; k++) {
							if (fieldsTo[j].getName().equals(fieldsFrom[k].getName())) {

								Method setter = toClass.getDeclaredMethod(
										"set" + captureName(fieldsTo[j].getName()), fieldsTo[j].getType());
								Method getter = fromClass
										.getDeclaredMethod("get" + captureName(fieldsFrom[k].getName()));
								Object value = getter.invoke(fromObject);
								if(value != null){
									setter.invoke(toObject, getter.invoke(fromObject));
								}else{
									String colName = generName(fieldsTo[j].getName());
									setter.invoke(toObject, fromObject.getExtendProperties().get(colName));
								}
								match = true;
								break;
							}
						}
						if (!match) {
							String colName = generName(fieldsTo[j].getName());
							Method setter = toClass.getDeclaredMethod(
									"set" + captureName(fieldsTo[j].getName()), fieldsTo[j].getType());
							Object object = fromObject.getExtendProperties().get(colName);
							if(object != null){
								setter.invoke(toObject, fromObject.getExtendProperties().get(colName));
							}
							
						}
					} catch (Exception e) {}
				}
				resultList.add(toObject);
			}
		}
		return resultList;
	}

	public static String captureName(String name) {
		name = name.substring(0, 1).toUpperCase() + name.substring(1);
		return name;
	}

	public static String generName(String name) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < name.length(); i++) {
			char c = name.charAt(i);
			if (Character.isUpperCase(c)) {
				sb.append('_');
				sb.append((char)(c + 32));
			} else {
				sb.append(c);
			}
		}
		return sb.toString();
	}

}
